
quietly {

forv d=1/3 {

forv bs=0/$bootstraps {
		forv o=1/10 {
	

	clear
	use ${data}Simulation/sim_data_d`d'_bs`bs'.dta
	
	keep if oo==`o'
	
	* Draw initial values for thresholds
	gen rv = runiform(0,0.5) in 1/6
	 replace rv = 0 in 7   
	 // use outside option for last threshold
	gsort - rv
		
	forv r=1/7 {	
		gen tau`r'_1 = sch_outside_option + rv[`r']
	}	
	drop rv
	
	
		

	local l=0
	local max=1

	while `max'>=.01 & `l'<20 {

	local l=`l'+1



	if `l'>1 {
		local ll = `l'-1
		restore
		drop match_u* sim_hire_round*
		merge m:1 schoolpos_id  using `newtaus', nogen
		
		forv r=1/7 {
			gen diff`r' = abs(tau`r'_`l'-tau`r'_`ll')
		}
		egen max=rowmax(diff1 diff2 diff3 diff4 diff5 diff6 diff7)
		su max
		local max=r(max)
		drop max diff1 diff2 diff3 diff4 diff5 diff6 diff7
	}
	
	



		gen unhired = 1
		gen unfilled = 1


	
		gen _hire1 = .
			replace _hire1 = u_sch>=tau1_`l'  & u_sch<. & round==1 & pos==1 
		bys teacher_id: egen hire_t1 = max(_hire1)
		bys schoolpos_id: egen hire_s1 = max(_hire1)
		bys schoolcert_id: egen n_hires = sum(_hire1)
	
		replace unhired = 0 if hire_t1==1
		replace unfilled = 0 if hire_s1==1
	
		gen _hire2 = .
			replace _hire2 = u_sch>=tau2_`l' & u_sch<. & round==2  & (pos==1 | pos==2 & n_hires==1) & unhired==1 & unfilled==1 
		bys teacher_id: egen hire_t2 = max(_hire2)
		bys schoolpos_id: egen hire_s2 = max(_hire2)
		bys schoolcert_id: egen _n_hires = sum(_hire2)
		replace n_hires = n_hires+_n_hires
		drop _n_hires
		replace unhired = 0 if hire_t2==1
		replace unfilled = 0 if hire_s2==1	
	
		gen _hire3 = .
			replace _hire3 = u_sch>=tau3_`l' & u_sch<. & round==3 & (pos==1 | pos==2 & n_hires==1)  & unhired==1 & unfilled==1 
		bys teacher_id: egen hire_t3 = max(_hire3)
		bys schoolpos_id: egen hire_s3 = max(_hire3)
		bys schoolcert_id: egen _n_hires = sum(_hire3)
		replace n_hires = n_hires+_n_hires
		drop _n_hires
		replace unhired = 0 if hire_t3==1
		replace unfilled = 0 if hire_s3==1	
	
	
		gen _hire4 = .
			replace _hire4 = u_sch>=tau4_`l' & u_sch<. & round==4   & (pos==1 | pos==2 & n_hires==1)  & unhired==1 & unfilled==1 
		bys teacher_id: egen hire_t4 = max(_hire4)
		bys schoolpos_id: egen hire_s4 = max(_hire4)
		bys schoolcert_id: egen _n_hires = sum(_hire4)
		replace n_hires = n_hires+_n_hires
		drop _n_hires
		replace unhired = 0 if hire_t4==1
		replace unfilled = 0 if hire_s4==1	
	
		gen _hire5 = .
			replace _hire5 = u_sch>=tau5_`l' & u_sch<. & round==5 & (pos==1 | pos==2 & n_hires==1)  & unhired==1 & unfilled==1
		bys teacher_id: egen hire_t5 = max(_hire5)
		bys schoolpos_id: egen hire_s5 = max(_hire5)
		bys schoolcert_id: egen _n_hires = sum(_hire5)
		replace n_hires = n_hires+_n_hires
		drop _n_hires
		replace unhired = 0 if hire_t5==1
		replace unfilled = 0 if hire_s5==1	
	
		gen _hire6 = .
			replace _hire6 = u_sch>=tau6_`l' & u_sch<. & round==6 & (pos==1 | pos==2 & n_hires==1)  & unhired==1 & unfilled==1 
		bys teacher_id: egen hire_t6 = max(_hire6)
		bys schoolpos_id: egen hire_s6 = max(_hire6)
		bys schoolcert_id: egen _n_hires = sum(_hire6)
		replace n_hires = n_hires+_n_hires
		drop _n_hires
		replace unhired = 0 if hire_t6==1
		replace unfilled = 0 if hire_s6==1		

		gen _hire7 = .
			replace _hire7 = u_sch>=tau7_`l' & u_sch<.  & round==7  & (pos==1 | pos==2 & n_hires==1)  & unhired==1 & unfilled==1 
		bys teacher_id: egen hire_t7 = max(_hire7)
		bys schoolpos_id: egen hire_s7 = max(_hire7)
		replace unhired = 0 if hire_t7==1
		replace unfilled = 0 if hire_s7==1	


		gen _sim_hire_round = hire_s1+2*hire_s2+3*hire_s3+4*hire_s4+5*hire_s5+6*hire_s6+7*hire_s7 
		bys schoolpos_id: egen sim_hire_round=max(_sim_hire_round)
		replace sim_hire_round = 8 if unfilled==1


		gen sim_match = .
		replace sim_match  =  (_hire1==1 | _hire2==1  | _hire3==1 | _hire4==1 | _hire5==1 | _hire6==1 | _hire7==1) & u_sch>sch_outside_option

		gen _match_u = u_sch if sim_match==1

		bys schoolpos_id: egen match_u_sch = max(_match_u)
		replace match_u_sch = sch_outside_option if unfilled==1 
 
		drop unhired-_sim_hire_round sim_match _match_u
 
 
		
		preserve
		keep schoolpos_id   match_u* sim_hire_round* sch_outside_option group

		local nl=`l'+1
		forv r=1/7 {
			gen tau`r'_`nl' = .
			qui su schoolpos_id
			local n=r(max)
			forv g=1/18 {
				qui su match_u if sim_hire_round>`r' & group==`g'
				replace tau`r'_`nl' = r(mean) if group==`g'
			}
		}

		bys schoolpos_id: egen seq=seq()
		keep if seq==1
		keep schoolpos_id  tau*

		tempfile newtaus
		save `newtaus', replace
		
		

	}


	restore
	gen converge = `max'<0.01
	save ${data}Simulation/sim_res_d`d'_o`o'_bs`bs'.dta, replace


	}
}
}







forv bs=0/$bootstraps {

* Do 1 extra iteration because of how I code it, so use 2nd to last iteration
forv o=1/10 {
	
clear 
use ${data}Simulation/sim_res_d1_o`o'_bs`bs'.dta
forv m=1/100 {
	cap ds tau1_`m'
	if _rc==0 local max = `m'
}
local max=`max'-1

keep schoolpos_id tau*_`max' converge
forv r=1/7 {
	ren tau`r'_`max' tau`r'_star
	replace tau`r'_star = . if converge==0
}
by schoolpos_id: gen order = _n
keep if order==1
drop order
save ${data}Simulation/optimal_taus1_o`o'_bs`bs'.dta, replace



clear 
use ${data}Simulation/sim_res_d2_o`o'_bs`bs'.dta
forv m=1/100 {
	cap ds tau1_`m'
	if _rc==0 local max = `m'
}
local max=`max'-1

keep schoolpos_id tau*_`max' converge
forv r=1/7 {
	ren tau`r'_`max' tau`r'_star 
	replace tau`r'_star = . if converge==0
}
by schoolpos_id: gen order = _n
keep if order==1
drop order
save ${data}Simulation/optimal_taus2_o`o'_bs`bs'.dta, replace


clear 
use ${data}Simulation/sim_res_d3_o`o'_bs`bs'.dta
forv m=1/100 {
	cap ds tau1_`m'
	if _rc==0 local max = `m'
}
local max=`max'-1

keep schoolpos_id tau*_`max' converge

forv r=1/7 {
	ren tau`r'_`max' tau`r'_star
	replace tau`r'_star = . if converge==0
}
by schoolpos_id: gen order = _n
keep if order==1
drop order
save ${data}Simulation/optimal_taus3_o`o'_bs`bs'.dta, replace
}
}


exit
